package com.example.cadmin.service;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.cadmin.entity.Host;
import com.example.cadmin.mapper.HostMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cadmin.utils.ResponseModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 机柜表 服务实现类
 * </p>
 *
 * @author zzsoft
 * @since 2023-01-05
 */
@Service
public class HostService extends ServiceImpl<HostMapper, Host> {

    @Resource
    private HostMapper hostMapper;

    /**
     * 系统 ip表格
     */
    public Map<String, Object> hostIpList(int page, int limit) {
        IPage<Map<String, Object>> hostPage = new Page<>(page, limit);//参数一是当前页，参数二是每页个数
        hostPage = hostMapper.selectMapsPage(hostPage, null);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "数据请求成功");
        map.put("count", hostPage.getTotal());
        map.put("data", hostPage.getRecords());
        return map;
    }


    /**
     * 新增主机保存
     */
    public ResponseModel hostAddSave(JSONObject data) {
        Host host = new Host();
        host.setNumber(data.getStr("number"));
        host.setIp(data.getStr("ip"));
        host.setNumberOfLayers(data.getStr("layer"));
        host.setNumberOfColumns(data.getStr("column"));
        int status = hostMapper.insert(host);
        return status > 0 ? ResponseModel.success("新增主机信息成功") : ResponseModel.fail("新增主机信息失败");
    }


    /**
     * 主机IP信息修改保存
     *
     * @param data
     * @return
     */
    public ResponseModel hostEditSave(JSONObject data) {
        UpdateWrapper<Host> hostUpdateWrapper = new UpdateWrapper<>();
        hostUpdateWrapper.eq("id", data.getStr("id"))
                .set("ip", data.getStr("ip"))
                .set("number", data.getStr("number"))
                .set("numberOfLayers", data.getStr("layer"))
                .set("numberOfColumns", data.getStr("column"));
        int status = hostMapper.update(null, hostUpdateWrapper);
        return status > 0 ? ResponseModel.success("修改主机信息成功") : ResponseModel.fail("修改主机信息失败");
    }


    /**
     * 主机IP信息删除
     * @return
     */
    public ResponseModel hostRemoveId(String id) {
        int status = hostMapper.deleteById(id);
        return status > 0 ? ResponseModel.success("删除主机信息成功") : ResponseModel.fail("删除主机信息失败");
    }

}
